Public Class OrderStatusDataFilling
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents btnOK As System.Web.UI.WebControls.Button
    Protected WithEvents btnCancel As System.Web.UI.WebControls.Button
    Protected WithEvents lblInfo As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Private Const ORDER_LIST_URL As String = "/Internal/OrderList.aspx"
    Private Const CANNOT_UPDATE_ERR_MSG As String = "Can not update order status. Reason: the last updated date of order is greater than current date"
    Private _cntOrderMissing As Integer
    Private _cntOrderFilled As Integer


#Region "   Properties"
    Private ReadOnly Property ReturnURL() As String
        Get
            If IsNothing(Request.QueryString("ru")) Then
                Return AppRoot() & Me.ORDER_LIST_URL
            End If
            Return Server.UrlDecode(Request.QueryString("ru"))
        End Get
    End Property

    Private Property UrlReferrer() As String
        Get
            Return CStr(ViewState("UrlReferrer"))
        End Get
        Set(ByVal Value As String)
            ViewState("UrlReferrer") = Value
        End Set
    End Property

    'Protected ReadOnly Property OrderID() As Integer
    '    Get
    '        Try
    '            If IsNothing(Request.QueryString("id")) Then
    '                Throw New Exception
    '            End If

    '            Dim id As Integer
    '            id = CInt(Request.QueryString("id"))

    '            If Not id > 0 Then
    '                Throw New Exception
    '            End If

    '            Return id
    '        Catch ex As Exception
    '            Throw New ApplicationException("Invalid or missing parameter", ex)
    '        End Try
    '    End Get
    'End Property

    Protected ReadOnly Property CustomerID() As Integer
        Get
            Try
                If IsNothing(Request.QueryString("id")) Then
                    Throw New Exception
                End If

                Dim id As Integer
                id = CInt(Request.QueryString("id"))

                If Not id > 0 Then
                    Throw New Exception
                End If

                Return id
            Catch ex As Exception
                Throw New ApplicationException("Invalid or missing parameter", ex)
            End Try
        End Get
    End Property

    Protected Property MissingOSList() As ArrayList
        Get
            Return CType(Page.Session("MissingOSList"), ArrayList)
        End Get
        Set(ByVal Value As ArrayList)
            Session("MissingOSList") = Value
        End Set
    End Property

#End Region

    Private Sub RedirectToOrderStatusUpdatePage()
        Response.Redirect(AppRoot() & "/Internal/OrderStatusUpdate.aspx" & Request.Url.Query)
    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
            Security.InternalAndAdminOnly()
            'Dim url As String = Request.UrlReferrer.AbsolutePath
            'If url.IndexOf("OrderStatusUpdate.aspx") = -1 Then
            '    Throw New ApplicationException("This page can only be called from Order Status Update page")
            'End If
            Dim colMissingOS As New ArrayList
            Dim dt As DataTable = COrder.GetAllPurchaseOrderActiveForCustomer(Me.CustomerID)
            Dim cnt As Integer = dt.Rows.Count
            Dim row As DataRow
            For Each row In dt.Rows
                Dim id As Integer = Convert.ToInt32(row(0))
                Dim cntWeek As Integer = 0
                Dim objOS As New COrderStatus
                objOS.LoadLastestOrderStatus(id)

                If objOS.ObjectState.Tables(0).Rows.Count > 0 Then
                    If objOS.Year = Year(Now) Then

                        If objOS.Week > Week(Now) Then
                            Throw New ApplicationException(Me.CANNOT_UPDATE_ERR_MSG)
                        End If

                        If objOS.Week = Week(Now) Or objOS.Week = Week(Now) - 1 Then
                            'Me.RedirectToOrderStatusUpdatePage()
                            Me._cntOrderFilled += 1
                        Else
                            cntWeek = Week(Now) - objOS.Week
                        End If


                    ElseIf Year(Now) > objOS.Year Then
                        If Year(Now) - objOS.Year = 1 And objOS.Week = 52 And Week(Now) = 1 Then
                            'Me.RedirectToOrderStatusUpdatePage()
                            Me._cntOrderFilled += 1
                        Else
                            cntWeek = (Year(Now) - objOS.Year - 1) * 52 + Week(Now) + (52 - objOS.Week)
                        End If
                    Else
                        Throw New ApplicationException(Me.CANNOT_UPDATE_ERR_MSG)
                    End If
                Else
                    'Me.RedirectToOrderStatusUpdatePage()
                    Me._cntOrderFilled += 1
                End If

                If cntWeek <> 0 Then
                    colMissingOS.Add(objOS)
                End If
                'If objOS.ObjectState.Tables(0).Rows.Count > 0 Then
                '    If objOS.Year = Year(Now) Then
                '        If objOS.Week = Week(Now) Or objOS.Week = Week(Now) - 1 Then
                '            Me.RedirectToOrderStatusUpdatePage()
                '        End If
                '        If objOS.Week > Week(Now) Then
                '            Throw New ApplicationException(Me.CANNOT_UPDATE_ERR_MSG)
                '        End If
                '        cntWeek = Week(Now) - objOS.Week
                '    ElseIf Year(Now) > objOS.Year Then
                '        If Year(Now) - objOS.Year = 1 And objOS.Week = 52 And Week(Now) = 1 Then
                '            Me.RedirectToOrderStatusUpdatePage()
                '        End If

                '        cntWeek = (Year(Now) - objOS.Year - 1) * 52 + Week(Now) + (52 - objOS.Week)
                '    Else
                '        Throw New ApplicationException(Me.CANNOT_UPDATE_ERR_MSG)
                '    End If
                'Else
                '    Me.RedirectToOrderStatusUpdatePage()
                'End If
            Next

            If Me._cntOrderFilled = cnt Then
                Me.RedirectToOrderStatusUpdatePage()
            Else
                'Me.lblInfo.Text = String.Format("The system has found {0} missing week(s) of order status data from last updated week (Week {1} / Year {2}).<BR> To continue, the system must fill-in the missing with data from that week.", cntWeek - 1, objOS.Week, objOS.Year)
                Me.MissingOSList = colMissingOS
                Dim msg As String = ""
                msg = msg & String.Format("The system has found {0} order(s) that have missing week(s) of order status data.<BR>", Me.MissingOSList.Count)
                Dim obj As COrderStatus
                For Each obj In Me.MissingOSList
                    Dim objO As New COrder
                    Try
                        objO.Load(obj.OrderID)
                    Catch ex As Exception
                    End Try
                    msg = msg & String.Format("<li> PO#: {0} has missing week(s) of order status data from last update week (Week {1} / Year {2}).<BR>", objO.OrderNumber, obj.Week, obj.Year)
                Next
                msg = msg & "To continue, the system must fill-in the missing with data from that week."
                Me.lblInfo.Text = msg
            End If

        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect(Me.ReturnURL)
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim obj As COrderStatus
        For Each obj In Me.MissingOSList
            COrderStatus.FillinMissingOrderStatus(obj.OrderID, Now)
        Next        
        Me.RedirectToOrderStatusUpdatePage()
    End Sub
End Class
